package com.tongtech.space.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * 公共工具包
 *
 * @author jinjiayu
 * @since 2021/1/14
 */
@Slf4j
public class CommonUtils {

    public static String getIpAddress(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if ("127.0.0.1".equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
                //根据网卡取本机配置的IP
                try {
                    ipAddress = InetAddress.getLocalHost().getHostAddress();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            }
        }
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        //"***.***.***.***".length() = 15
        if (ipAddress != null && ipAddress.length() > 15) {
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }

    /**
     * 证照脱敏
     * 只保留字符串第一位和最后一位, 例如3*****************4
     *
     * @param sourceString
     * @return String
     * @author jinjiayu
     */
    public static String certificateDesensitization(String sourceString) {
        StringBuilder resultString = new StringBuilder();
        if (StringUtils.isBlank(sourceString)) {
            return resultString.toString();
        }

        for (int i = 0; i < sourceString.length(); i++) {
            if (i == 0 || i == sourceString.length() - 1) {
                resultString.append(sourceString.charAt(i));
            } else {
                resultString.append("*");
            }
        }
        return resultString.toString();
    }


    /**
     * 获取restTemplate默认请求HttpEntity参数
     *
     * @param body headerMap
     * @return HttpEntity<E>
     * @author jinjiayu
     */
    public static <E> HttpEntity<E> getDefaultHttpEntity(E body, Map<String, String> headerMap) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        if (headerMap != null) {
            headers.setAll(headerMap);
        }
        return new HttpEntity<>(body, headers);
    }

    /**
     * 返回结果信息
     * @param result
     * @param message
     * @return
     */
    public static Map<String, Object> setResult(Integer result,Object message){
        Map<String, Object> mapResult = new HashMap<>();
        mapResult.put("result", result);
        mapResult.put("message", message);
        return mapResult;
    }
}
